package SuanFaTest;

public class ErFenSearch {
    private static int binarySearch(int[] array ,int target){
        int low = 0;
        int high = array.length - 1;
        int mid = (low + high) / 2;
        int index = -1;//值为-1说明数组中没有中间变量，，有的话返回mid值
        for(int i = 0; i < array.length - 1; i++){//冒泡排序，对数组进行排序
            for(int j = 0; j < array.length - 1 - i; j++){
                if (array[j] > array[j + 1]){
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        while (low <= high){
            if (target == array[mid]){
                index = mid;
                break;
            }
            if (target > array[mid]){
                low = mid + 1;
            } else if (target < array[mid]){
                high = mid - 1;
            }
            mid = (low + high) / 2;
        }
        return index;//找到了返回mid找不到在这里返回
    }
}
